package servlet;

import entity.Goods;
import entity.Type;
import entity.User;
import service.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "GoodsServlet",urlPatterns = "/GoodsServlet")
public class GoosServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String opr = request.getParameter("opr");
        HttpSession session = request.getSession();
        UserService userService = new UserServiceImpl();
        GoodsService goodsService = new GoodsServiceImpl();
        TypeService typeService = new TypeServiceImpl();
        //int id = Integer.parseInt(request.getParameter("id"));
        String keyword = (request.getParameter("keyword")!=null)?request.getParameter("keyword").trim():"";

        if (opr.equals("goods1")){
            Goods goods1 = goodsService.getCommod();
            List<Goods> goods = goodsService.getCommodity(keyword);
            List<Goods> newList = goodsService.getProduct(keyword);
            List<Type> type = typeService.getSeries(keyword);
            session.setAttribute("type",type);
            request.setAttribute("goods1",goods1);
            session.setAttribute("goods", goods);
            session.setAttribute("newList",newList);
            request.setAttribute("keyword",keyword);
            request.getRequestDispatcher("index.jsp").forward(request, response);
        }else if(opr.equals("goods3")){
            int id = Integer.parseInt(request.getParameter("id"));
            Goods goods3 = goodsService.getParticulars(id);
            List<Type> type = typeService.getSeries(keyword);
            session.setAttribute("type",type);
            session.setAttribute("goods3",goods3);
            request.getRequestDispatcher("goods_detail.jsp").forward(request, response);
        }else if (opr.equals("detail")){
            int type =Integer.parseInt(request.getParameter("type"));
            request.setAttribute("t",type);
            if (type==2){
                List<Goods> detail = goodsService.getCommodity(keyword);
                session.setAttribute("p",detail);
            }else {
                List<Goods> product = goodsService.getProduct(keyword);
                session.setAttribute("p",product);
            }
            request.getRequestDispatcher("goodsrecommend_list.jsp").forward(request, response);
        }else if (opr.equals("goods_list")){
            List<Type> type = typeService.getSeries(keyword);
            int typeid = Integer.parseInt(request.getParameter("typeid"));
            if (typeid!=0){
                List<Goods> series = goodsService.getSeries(typeid);
                session.setAttribute("type",type);
                session.setAttribute("series",series);

            }
            List<Goods> series = goodsService.getSeries(typeid);
            session.setAttribute("type",type);
            session.setAttribute("series",series);
            request.getRequestDispatcher("goods_list.jsp").forward(request, response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        doGet(req,resp);
    }
}
